//
//  WordDetailIPadViewController.m
//  EnglishStudyApp
//
//  Created by Nong Trung Nghia on 10/18/12.
//  Copyright (c) 2012 abc. All rights reserved.
//

#import "WordDetailIPadViewController.h"
#import "WordDetailView.h"
#import "ESWord.h"

#import <AVFoundation/AVFoundation.h>


@implementation WordDetailIPadViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
//        self.view.backgroundColor=[UIColor brownColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _wordDetailView=[[WordDetailView alloc] initWithFrame:self.view.frame];
    _wordDetailView.delegate=self;
    self.view=_wordDetailView;
    self.title=@"Word Detail";
    [self shouldAutorotateToInterfaceOrientation:[[UIDevice currentDevice] orientation]];
}

-(void)setWord:(ESWord*)word{
    [_wordDetailView setWord:word];
    NSError *error=nil;
    _imageAudioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:[[word.imageSound componentsSeparatedByString:@"."] objectAtIndex:0] withExtension:[[word.imageSound componentsSeparatedByString:@"."] objectAtIndex:1]] error:&error];
    if (error) {
        NSLog(@"%@",error.localizedDescription);
    }
    
    error=nil;
    
    _descriptionAudioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:[[word.textSound componentsSeparatedByString:@"."] objectAtIndex:0] withExtension:[[word.textSound componentsSeparatedByString:@"."] objectAtIndex:1]] error:&error];
    if (error) {
        NSLog(@"%@",error.localizedDescription);
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(3_0){
    
}
#pragma mark - Word Detail Delegate
-(void)wordDetailView:(WordDetailView*)wordDetailView didSelectImageView:(UIImageView*)imageView{
    NSLog(@"image");
    if (![_imageAudioPlayer isPlaying]&&![_descriptionAudioPlayer isPlaying]) {
        [_imageAudioPlayer play];

    }
}
-(void)wordDetailView:(WordDetailView*)wordDetailView didSelectVNTextLabel:(UILabel*)vnTextLabel{
    NSLog(@"vn text");
    if (![_imageAudioPlayer isPlaying]&&![_descriptionAudioPlayer isPlaying]) {
        [_descriptionAudioPlayer play];
    }    
}
-(void)wordDetailView:(WordDetailView*)wordDetailView didSelectENTextLabel:(UILabel*)enTextLabel{
    NSLog(@"en text");
    if (![_imageAudioPlayer isPlaying]&&![_descriptionAudioPlayer isPlaying]) {
        [_descriptionAudioPlayer play];
    }
    
}



@end
